# !/usr/bin python3                                
# encoding: utf-8 -*-
# @file: test_topic.py                       
# @author   : wjd
# @Time: 2024/4/1 20:08

"""
习题：
1、去掉字符串的前后空格
2、去掉字符串中间的空格字符
3、将字符串中的大写字母转成小写
4、将字符串中的小写字母转成大写
5、将字符串中的大写字母转成小写，小写字母转成大写
6、统计字符串中的字母、数字、其他字符个数
7、统计名字列表中，各名字的首字母在名字列表中出现的次数
8、一个字符串中，分别输出奇数坐标字符或偶数坐标字符，奇数坐标的一行，偶数坐标的一行
9、字符串包含数字则1替换成a，2替换成b，3替换成c，以此类推
10、将字符串使用空格进行切分，存到一个列表，然后使用*号连接，并输出
11、找出字符串中出现次数最多的字符，并输出其出现的位置
12、找出一段句子中最长的单词及其索引位置
"""
import re


def string_strip(s):
    """
    去掉字符串的前后空格
    :param s:
    :return:
    """
    return s.strip()


def string_replace(s):
    """
    去掉字符串中间的空格字符
    :param s:
    :return:
    """
    return s.replace(' ', '')


def string_upper(s):
    """
    将字符串中的大写字母转成小写
    :param s:
    :return:
    """
    return s.upper()


def string_lower(s):
    """
    将字符串中的小写字母转成大写
    :param s:
    :return:
    """
    return s.lower()


def string_swapcase(s):
    """
    将字符串中的大写字母转成小写，小写字母转成大写
    :param s:
    :return:
    """
    return s.swapcase()


def string_count(s):
    """
    统计字符串中的字母、数字、其他字符个数
    :param s:
    :return:
    """
    # str.isalpha()
    str_list = [i for i in s if i in re.findall("[a-zA-Z]", s)]
    nums_list = [i for i in s if i in re.findall("\d", s)]
    print(len(str_list))
    print(len(nums_list))
    special_list = [i for i in s if i not in str_list and i not in nums_list]
    print(len(special_list))


def string_first_count(s: list):
    """
    统计名字列表中，各名字的首字母 在名字列表中出现的次数
    :param s:
    :return:
    """
    for i in s:
        nums = 0
        for t in s:
            nums += t.count(i[0])
        print(f"{i[0]}出现的次数是:{nums}")


def string_count_list(s: str):
    """
    "12345678901234567890"
    一个字符串中，分别输出奇数坐标字符或偶数坐标字符，奇数坐标的一行，偶数坐标的一行
    :param s:
    :return:
    """
    list_even = []  # 奇数下标
    list_even_subscript = []  # 奇数下标
    list_ood = []  # 偶数下标
    list_ood_subscript = []  # 偶数下标
    for i in s:
        if s.index(i) % 2 == 1:
            # 判断下标是奇数
            list_even.append(i)
            list_even_subscript.append(s.index(i))
        elif s.index(i) % 2 == 0:
            # 判断下标是偶数
            list_ood.append(i)
            list_ood_subscript.append(s.index(i))
    print(f"奇数下标list:{list_even}")
    print(f"奇数下标:{list_even_subscript}")
    print(f"偶数下标list:{list_ood}")
    print(f"偶数下标:{list_ood_subscript}")


def string_replace_number(s: str):
    """
    字符串包含数字则1替换成a，2替换成b，3替换成c，以此类推
    :param s:
    :return:
    """
    a = list(s)
    print(a)
    list_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    list_str = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
    for i in s:
        try:
            i = int(i)
            if i in list_number:
                a[a.index(f"{i}")] = list_str[list_number.index(i)]
        except:
            pass
    print(a)


def string_join(s: str):
    """
    10、将字符串使用空格进行切分，存到一个列表，然后使用*号连接，并输出
    :param s:
    :return:
    """
    list1 = s.split(" ")
    print(list1)
    print("*".join(list1))


def string_find_count(s):
    """
    找出字符串中出现次数最多的字符，并输出其出现的位置
    :param s:
    :return:
    """
    dict1 = {}
    for i in s:
        s.count(i)
        dict1[i] = s.count(i)
    max1 = max(dict1.values())
    key1 = [k for k, v in dict1.items() if v == max1]
    print(key1)
    count = 0
    while count < len(s):
        if s[count] == key1[0]:
            print(count)
        count += 1


"hello world 123,is very good,is very bad"


def string_find_long(s: str):
    """
    # 12、找出一段句子中最长的单词及其索引位置
    :return:
    """

    list1 = s.replace(",", " ").split(" ")
    print(list1)
    a = [i for i in list1 if len(i) == max([len(i) for i in list1])]
    print(a)
    for i in a:
        print(list1.index(i))


if __name__ == '__main__':
    # s = "  hello world  "
    # print(string_strip(s))
    # print(string_replace(s))
    # print(string_upper(s))
    # print(string_lower(s))
    # print(string_swapcase(s))

    # s = "hello world 123 !@#"
    # string_count(s)

    # s = ["zhangsan", "lisi", "wangwu", "zhaoliu", "wanger", "lili", "wuwu", "qianqian", "lili", "maomao", "dali"]
    # string_count(s)

    # s = "12345678901234567890"
    # string_count_list(s)

    # s = "132324f5678s90a1234567g890"
    # string_replace_number(s)

    # s = "123 21 23 12312 3"
    # string_join(s)

    # s = "12312342424252535454"
    # string_find_count(s)

    # s = "12312342424252535454"
    # string_find_count(s)

    s = "hello  123,is very world good,is very bad"
    string_find_long(s)
